<template>
  <!-- <div> -->
    <el-dialog
    append-to-body
      title="币种信息"
      :visible.sync="applyDialog"
      style="width:30%;text-align:center;margin:0 auto;"
    >
      <el-form
        :model="reviewForm"
        :rules="rules"
        ref="reviewForm"
        label-width="40%"
        style="width:160%;text-align:center;margin:0 auto"
      >
        <el-row style="text-align:center;margin:0 auto">
          <el-col :span="12">
            <el-form-item label="请选择币种" prop="currencyCode">
              <el-select
                size="mini"
                v-model="reviewForm.currencyCode"
                style="width:110%;text-align:center;margin:0 auto"
                clearable
              >
                <el-option
                  v-for="(v, i) in CurrencyTypeSource"
                  :key="i"
                  :value="i"
                  :label="v"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button size="mini" @click="applyDialog = false">取消</el-button>
        <el-button size="mini" type="primary" @click="batchReview('reviewForm')"
          >确定</el-button
        >
      </div>
    </el-dialog>
  <!-- </div> -->
</template>
<script>
import { ajax } from "@/store/yg/common/utils";
import axios from "axios";
import * as constant from "@/store/yg/common/constant";

export default {
  props: ["row", "data", "more"],
  data() {
    return {
      reviewForm: {
        currencyCode: "RMB"
      },
      rules: {
        currencyCode: [
          { required: true, message: "请选择币种", trigger: "change" }
        ]
      },
      CurrencyTypeSource: {
        currencyCode: "RMB"
      },
      applyDialog: false
    };
  },

  methods: {
    async batchReview() {
      if (!this.reviewForm.currencyCode) {
        this.$message.error("请选择币种");
        return;
      }
      const ids = this.$store.state.edit.ids;
      console.log("=====================>" + ids);
      //console.log(this.$store.state.main.selectedData);
      const selectedData = this.$store.state.main.selectedData;
      if (selectedData && selectedData.length > 0) {
        var customerIds = new Array();
        var yes = true;
        var rateYes = true;
        var result = JSON.parse(selectedData[0].rateJson);
        //console.log("result",result);
        const customerId = selectedData[0].customerId;
        if (ids.length > 1) {
          let newIds = [];
          newIds = ids.toString().split(",");
          newIds.shift();
          //console.log("newIds",newIds);
          let res = await ajax("initCurrencyData", newIds);
          selectedData.forEach(item => {
            var resultJson = JSON.parse(item.rateJson);
            for (const key in result) {
              if (res.includes(key)) {
                if (result[key].rate != resultJson[key].rate) {
                  rateYes = false;
                }
              }
            }
            if (customerId != item.customerId) {
              yes = false;
              return;
            } else {
              customerIds.push(item.customerId);
            }
          });
        }

        if (!yes) {
          this.$message.error("相同客户的账单才允许审核");
          return;
        } else if (!rateYes) {
          this.$message.error("汇率相同的账单才允许审核");
          return;
        } else {
          window.open(
            "#/blank/finance__account__main?customerBillIdStrs=" +
              ids +
              "&currencyCode=" +
              this.reviewForm.currencyCode
          );
        }
      }
    },
    initCurryOptions(bean) {
      var uri = "/api/entry/source?bean=" + bean;
      axios.post(constant.API_SERVER + uri).then(
        res => {
          this.CurrencyTypeSource = res.data;
        },
        error => {
          console.info(error);
        }
      );
    }
  },
  mounted() {
    this.initCurryOptions("currencyTypeSource");
    this.applyDialog = true;
  }
};
</script>
<style scoped></style>

<style lang="scss">
// .row-item {
//   color: #3f4652;
//   line-height: 28px;
//   margin-bottom: 14px;
// }
</style>
